package me.spring.cloud.common.components.exception.exceptions;

import lombok.Data;
import me.spring.cloud.common.components.exception.handler.ExceptionHandler;
import me.spring.cloud.common.components.response.util.CommonResponseUtil;
import me.spring.cloud.common.components.rpc.response.CommonResponse;
import me.spring.cloud.common.components.util.enums.CodedEnum;

/**
 * 带编码的异常
 * @author luffy
 */
@Data
public class CodedRuntimeException extends RuntimeException implements ExceptionHandler {

  private static final long serialVersionUID = 7456916389026483831L;

  private CodedEnum errorCode;

  public CodedRuntimeException(CodedEnum code) {
    super(code.getDesc());
    this.errorCode = code;
  }

  public CodedRuntimeException(CodedEnum code, String message) {
    super(message);
    this.errorCode = code;
  }

  public CodedRuntimeException(CodedEnum code, Throwable t) {
    super(t);
    this.errorCode = code;
  }

  @Override
  public CommonResponse<?> handle() {
    return CommonResponseUtil.error(this.errorCode);
  }

  @Override
  public CommonResponse<?> handle(String message) {
    return CommonResponseUtil.error(this.errorCode,message);
  }

}
